package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.PrintBusinessBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PrintBusinessMapper;
import com.koron.css2.serviceManage.queryBean.PrintBusinessQueryBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PrintBusinessQuery implements ServerInterface {

	@SuppressWarnings("rawtypes")
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<PrintBusinessBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PrintBusinessBean.class);
		try {
			PrintBusinessQueryBean qbean = JsonUtils.objectToPojo(req.getData(), PrintBusinessQueryBean.class);
			PrintBusinessMapper mapper = factory.getMapper(PrintBusinessMapper.class);
			PrintBusinessBean bean = mapper.selectByPrimaryKey(qbean.getId());
			// 20220617: 兼容数据为空的情况;但讲道理，数据库明明是有数据的。
			List<HashMap> list = new ArrayList<>();
			if (StringUtils.isNotEmpty(bean.getFields())) {
				list = JsonUtils.jsonToList(bean.getFields(), HashMap.class);
			}
			bean.setFieldsList(list);
			info.setData(bean);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
